set.seed(1782318297)

library(pacman)
library(tidyverse)

##Alternate explanations


#Figure 6

install.packages("remotes")
remotes::install_github("xiangzhou09/rwrmed")

library(rwrmed)

pre_cov <- c("age", "female", "education", "income", "race", "urban",
             "country")

m_form_alt <- sup ~ age + female + education + income + race + urban + country + vic + aoj11

y_form_alt <- sat ~ sup + age + female + education + income + race + urban + country + vic + 
  aoj11*sup + wave

fit_full_alt <- rwrmed(treatment = "vic", pre_cov = pre_cov, zmodels = list(m2),
                       y_form = y_form_alt, m_form = m_form_alt, weights = weights, data = full_data)

fit_full_alt <- decomp(fit_full_alt, rep = 500)

alt_data <- fit_full_alt[["twocomp"]]

alt_data <- as.data.frame(alt_data)

alt_data <- alt_data %>% 
  rownames_to_column(var = "effect_type")

alt_plot <- alt_data %>% 
  ggplot(aes(x = Estimate, y = effect_type)) +
  geom_point() +
  geom_pointrange(aes(xmin = `2.5% Perc`, xmax = `97.5% Perc`)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  labs(y = "", x = "") +
  theme_minimal() +
  theme(text=element_text(size=11,  family="Cambria"))

alt_plot

#Figure 7

y_alt <- lm(sup ~ age + female + education + income + race + urban + country + vic, weights = weights,
            data = full_data)

summary(y_alt)


m_alt <- lm(sat ~ age + female + education + income + race + urban + country + vic, weights = weights,
            data = full_data)

summary(m_alt)

pacman::p_load(sjPlot)

plot_models(
  m_alt, y_alt, rm.terms = c("age", "female", "education", "income", "race", "urban", "country"),
  axis.labels = "Victimization",
  m.labels = c("Satisfaction with democracy", "Support for democracy"),
  show.values = FALSE, show.p = FALSE) + 
  geom_hline(yintercept = 0, color = "black", linetype = "dashed") +
  labs(y = "", x = "") +
  scale_colour_grey(start = 0, end = .7) +
  theme_minimal() + theme(text=element_text(size=11,  family="Cambria"))
